package com.yixun.qfbao.hander;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yixun.qfbao.api.service.OrganizationService;
import com.yixun.qfbao.apidto.GetAgentOrgByCompanyListReqDto;
import com.yixun.qfbao.apidto.GetAgentOrgByCompanyListResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.util.exception.TServerException;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.GetAgentOrgByCompanyListReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;

/**
 * 获取中介公司（查询出与楼盘合作的相关中介公司，带有模糊搜索）
 * @author xiaojin.li
 *
 */
@Service("getAgentOrgByCompanyListHander")
public class GetAgentOrgByCompanyListHander extends CommonAbstractHander {
	private static Logger logger= LoggerFactory.getLogger(GetAgentOrgByCompanyListHander.class);
	
	@Autowired
	private OrganizationService organizationService;
	@Autowired
	private LoginUtils loginUtils;
	
	@Override
	public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
		GetAgentOrgByCompanyListReqVo req = (GetAgentOrgByCompanyListReqVo)reqDto;
		GetAgentOrgByCompanyListReqDto reqDto1 = req.getParams();
		BaseResponseDTO<GetAgentOrgByCompanyListResDto> res = new BaseResponseDTO<GetAgentOrgByCompanyListResDto>();
		GetAgentOrgByCompanyListResDto resDto = new GetAgentOrgByCompanyListResDto();
		if(StringUtils.isNotBlank(reqDto1.getUserId())){
			PartnerInfo partnerInfo = loginUtils.getUser(Long.parseLong(reqDto1.getUserId()));
			reqDto1.setHigherId(partnerInfo.getHigherid());
		}
		try {
			resDto = organizationService.getAgentOrgByCompanyPage(reqDto1);
		}catch(TServerException e){
			resDto.setResult(ResultEnum.FAIL.getNo());
			resDto.setResultMsg(e.getErrorMsg());
		} catch (Exception e) {
			logger.error("GetAgentOrgByCompanyListHander error: {}", e);
			resDto.setResult(ResultEnum.FAIL.getNo());
			resDto.setResultMsg("系统异常");
		}
		res.setResData(resDto);
		res.setResult(resDto.getResult());
		return res;
	}
	
}
